module ActionController
  class Base
    class << self
      def register_exception( klass, handler )
        write_inheritable_hash 'handled_exceptions', klass => handler
      end
      
      def exception_handler_for( klass )
        unless read_inheritable_attribute( 'handled_exceptions' ).nil?
          read_inheritable_attribute( 'handled_exceptions' )[ klass ]
        end
      end
    end
    
    alias_method :perform_action_without_handled_exceptions, 
      :perform_action_without_rescue
      
    def perform_action_without_rescue
      perform_action_without_handled_exceptions
    rescue StandardError => e
      handler = self.class.exception_handler_for e.class
      if handler
        self.send handler
      else
        raise e
      end
    end
  end
end